#
# @lc app=leetcode.cn id=111 lang=python3
#
# [111] 二叉树的最小深度
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def minDepth(self, root: TreeNode) -> int:
        q = []
        q.append(root)
        depth = 1 if root is not None else 0

        while(q):
            # 将当前队列中的所有节点向四周扩散
            for _ in range(len(q)):
                cur = q.pop(0)
                if cur is None:
                    return depth
                # 判断是否到达终点
                if cur.left is None and cur.right is None:
                    return depth
                # 将cur的相邻节点加入队列
                if cur.left is not None:
                    q.append(cur.left)
                if cur.right is not None:
                    q.append(cur.right)

            # 在这里增加步数
            depth = depth + 1

        return depth


# @lc code=end
